/***************************************************************************//**
 * @file     bl_lin.h
 * @brief    This file is the header file of bootloader LIN configuration.
 * @version  V1.4.0
 * @date     July-2021
 * @author   Zhixin Semiconductor
 *
 * @note
 * Copyright (C) 2020-2022 Zhixin Semiconductor Ltd. All rights reserved.
 *
 ******************************************************************************/
#ifndef BL_LIN_H
#define BL_LIN_H

//#define Z20K11X_SDK
#ifdef Z20K11X_SDK
#include "Z20K11xM_uart.h"
#else
#include "uart_drv.h"
#endif

#include "Z20K11xM_sysctrl.h"
#include "Z20K11xM_gpio.h"
#include "Z20K11xM_clock.h"
#include "bl_message.h"

#define LIN_ID             UART0_ID
#define CLK_LIN_ID         CLK_UART0
#define SYSCTRL_LIN_ID     SYSCTRL_UART0
#define LIN_IRQ_NUM        UART0_IRQn 
#define LIN_SEND_MSG_ID    0x11U
#define LIN_REC_MSG_ID     0x12U
#define LIN_BUF_QUEUE_LEN   8U

#define LIN_REC_DATA_LEN   8U
#define LIN_SEND_DATA_LEN  3U

/* LIN IO definition */
#if (defined(DEV_Z20K118M))
#define  BL_LIN_SYS_CLK_PORT  CLK_PORTC
#define  BL_LIN_SYS_PORT      SYSCTRL_PORTC
#define  BL_LIN_PORT          PORT_C
#define  BL_LIN_TX_IO         GPIO_3
#define  BL_LIN_TX_PINMUX     PTC3_UART0_TX
#define  BL_LIN_RX_IO         GPIO_2
#define  BL_LIN_RX_PINMUX     PTC2_UART0_RX

#define  BL_LIN_SLP_CLK_PORT  CLK_PORTC
#define  BL_LIN_SLP_SYS_PORT  SYSCTRL_PORTC
#define  BL_LIN_SLP_PINMUX    PTC5_GPIO
#define  BL_LIN_SLP_IO_PORT   PORT_C
#define  BL_LIN_SLP_IO        GPIO_5

#elif (defined(DEV_Z20K116M))
#define  BL_LIN_SYS_CLK_PORT  CLK_PORTA
#define  BL_LIN_SYS_PORT      SYSCTRL_PORTA
#define  BL_LIN_PORT          PORT_A
#define  BL_LIN_TX_IO         GPIO_11
#define  BL_LIN_TX_PINMUX     PTA11_UART0_TX
#define  BL_LIN_RX_IO         GPIO_10
#define  BL_LIN_RX_PINMUX     PTA10_UART0_RX

#define  BL_LIN_SLP_CLK_PORT  CLK_PORTC
#define  BL_LIN_SLP_SYS_PORT  SYSCTRL_PORTC
#define  BL_LIN_SLP_PINMUX    PTC5_GPIO
#define  BL_LIN_SLP_IO_PORT   PORT_C
#define  BL_LIN_SLP_IO        GPIO_5

#else
    #error "No valid device defined!"
#endif

typedef enum
{
    LIN_STATE_IDLE = 0U,          /*!< Lin state: idle */
    LIN_STATE_RECEIVING_HEADER,   /*!< Lin state: receiving header */
    LIN_STATE_HEADER_RECEIVED,    /*!< Lin state: header has been received */
    LIN_STATE_HANDLING_RESPONSE,  /*!< Lin state: handling header */
    LIN_STATE_ERROR               /*!< Lin state: error */
} LinState_t;

typedef enum
{
    LIN_RESPONSE_NO_ACTION = 0U,   /*!< Lin response: no action */
    LIN_RESPONSE_TX,               /*!< Lin response: TX */
    LIN_RESPONSE_RX,               /*!< Lin response: RX */
} LinResponse_t;

typedef struct
{
    uint8_t dataLen;                    /*!< Length of data in bytes */ 
    uint8_t data[8U];                   /*!< Data bytes of the LIN message*/     
} LinRespBuf_t;

typedef struct
{
    uint32_t index;
    uint32_t header;
    uint32_t freeCnt;
    LinRespBuf_t  frames[LIN_BUF_QUEUE_LEN]; 
}LinBufQueue_t;

extern LinBufQueue_t linRxBufQueue;
extern LinBufQueue_t linTxBufQueue;

ResultStatus_t LinInit(void);
void LinHeaderDoneCbf(void);
void LinResponseDoneCbf(void);
void LinWriteTxQueue(uint8_t *data, uint8_t len);
void LinReset(void);
//FlagStatus_t LinReadOneFrame(MessageBuf_t *buf);
void LinHandleState(void);

#endif /* BL_LIN_H */